if facetviewAdsUrl && image.has('_relevantAds') && image.get('_relevantAds').length
  mixin ad_anchor(document, domain)
    - var displayUrl = document.url.replace(/http(s?):\/\//, '')

    if domain
      - var displayUrl = displayUrl.replace(domain, '')

    if displayUrl.length > 40
      - var displayUrl = displayUrl.substr(0, 15) + '...' + displayUrl.substr(-22)

    a(href=document.url, target='_blank', title=document.url)= displayUrl
    a(href=facetviewAdsUrl + '?query=resourcename_t_md:' + document.resourcename, target='_blank')
      span(style='float:right') FacetView

  - var relevantAds = image.get('_relevantAds')
  - var relevantAdInfo = image.get('_relevantAdInfo')

  if relevantAdInfo.showingNumAds < relevantAdInfo.totalNumAds
    h4 Ads (#{relevantAdInfo.totalNumAds} total, showing #{relevantAdInfo.showingNumAds})
  else
    h4 Ads (#{relevantAdInfo.totalNumAds} total)

  table.table-bordered.table#im-relevant-ads
    tbody
      for groupedDoc in image.get('_relevantAds')
        - var domain = groupedDoc[0]
        - var documents = groupedDoc[1]
        - var numDocuments = documents.length

        tr
          td(rowspan=numDocuments) #{domain.replace(/^www\./, '')} (#{numDocuments})
          td
            +ad_anchor(documents[0], domain)
        for doc in documents.slice(1)
          tr
            td
              +ad_anchor(doc, domain)
